local voucherId=ARGV[1]
local userId=ARGV[2]
local orderId=ARGV[3]

local stockKey="seckill:stock:"..voucherId
local orderKey="seckill:order:"..voucherId

--判断库存是否充足
if redis.call('get', stockKey)==nil then
    return 1
end
if tonumber(redis.call('get', stockKey))<=0 then
    return 1
end
--判断用户是否下过单
if redis.call('sismember',orderKey,userId)==1 then
    return 2
end
--扣库存
redis.call('incrby',stockKey,-1)
--保存下单用户
redis.call('sadd',orderKey,userId)
--发送消息到消息队列中
redis.call("xadd","stream.orders","*","userId",userId,"voucherId",voucherId,"id",orderId)
return 0;